<?php

/**
 * @package		Joomla.Tutorials
 * @subpackage	Component
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;

jimport('joomla.form.helper');

JFormHelper::loadFieldClass('list');

class JFormFieldType extends JFormFieldList
{
	protected $type = 'type';

	protected function getInput()
	{
    $onchange	= $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';
		$db = JFactory::getDBO();
		//$query = $db->getQuery(true);

    /*
     * SELECT concat(r.raffinerie, " ", f.famille, " ", t.format)
       FROM `d92i4_glycophile_type` as t,
       d92i4_glycophile_famille as f,
       d92i4_glycophile_raffinerie as r
       WHERE t.identraffinerie = r.id
       and t.identfamille = f.id
     */
		//$query->select('p.id as id, p.serie as serie');
		//$query->from('#__glycophile_type t');

    $query = 'SELECT t.id as id , concat(r.raffinerie, " (", f.famille, " - ", t.format, ")") as type
       FROM `#__glycophile_type` as t,
       #__glycophile_famille as f,
       #__glycophile_raffinerie as r
       WHERE t.identraffinerie = r.id
       and t.identfamille = f.id';

		$db->setQuery((string)$query);
 
		$types = $db->loadObjectList();
		$options = array();
		if($types){
			foreach($types as $type){
				$options[] = JHtml::_('select.option', $type->id, $type->type);
			}
		}
    
		$options = array_merge(parent::getOptions(), $options);
    $return = JHtml::_('select.genericlist', $options, $this->name, $onchange, 'value', 'text', $this->value, $this->value);
		return $return;
	}
}
